# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class JT_9201:
    result = []
    analysisDescription = ['服务器IP地址长度：', '服务器IP地址：', '服务器视频通道监听端口号(TCP)：',
                           '服务器视频通道监听端口号（UDP）：', '逻辑通道号：','音视频类型：','码流类型：','存储器类型：',
                           '回放类型：','快进或快退倍数：','开始时间：','结束时间：']
    analysis9201Length = []

    def LogicAisle(self, protocolStr, header):
        count = 0
        s = protocolStr[0:2]
        n = int(s,16)
        self.analysis9201Length=[1,n,2,2,1,1,1,1,1,1,6,6]
        for i in range(len(self.analysisDescription)):
            #服务器IP地址长度
            if i==0:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                s=int(s,16)
                header.append(self.analysisDescription[i] + str(s) + "\n")
                count = count + self.analysis9201Length[i] * 2
            #服务器IP地址
            elif i==1:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)].decode("hex").decode('gb2312')
                print(s)
                header.append(self.analysisDescription[i] + str(s) + "\n")
                count = count + self.analysis9201Length[i] * 2
            #TCP端口或者UDP端口
            elif i == 2 or i == 3:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                s = str(int(s, 16))
                header.append(self.analysisDescription[i] + s + "\n")
                count = count + self.analysis9201Length[i] * 2
            #音视频类型
            elif i==5:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                if s=="00":
                    header.append(self.analysisDescription[i]+s+" 音视频"+"\n")
                elif s=="01":
                    header.append(self.analysisDescription[i] + s + " 音频" + "\n")
                elif s=="02":
                    header.append(self.analysisDescription[i] + s + " 视频" + "\n")
                elif s=="03":
                    header.append(self.analysisDescription[i] + s +" 视频或音频" + "\n")
                count = count + self.analysis9201Length[i] * 2
            #码流类型
            elif i==6:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                if s == "00":
                    header.append(self.analysisDescription[i] + s + " 主码流或子码流"+ "\n")
                elif s == "01":
                    header.append(self.analysisDescription[i] + s + " 主码流" + "\n")
                elif s == "02":
                    header.append(self.analysisDescription[i] + s + " 子码流" + "\n")
                count = count + self.analysis9201Length[i] * 2
            #存储器类型
            elif i==7:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                if s == "00":
                    header.append(self.analysisDescription[i] + s + " 主存储器或灾备存储器" + "\n")
                elif s == "01":
                    header.append(self.analysisDescription[i] + s + " 主存储器" + "\n")
                elif s == "02":
                    header.append(self.analysisDescription[i] + s + " 灾备存储器" + "\n")
                count = count + self.analysis9201Length[i] * 2
            #回放方式
            elif i==8:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                if s=="00":
                    header.append(self.analysisDescription[i] + s + " 正常回放" + "\n")
                elif s=="01":
                    header.append(self.analysisDescription[i] + s + " 快进回放" + "\n")
                elif s=="02":
                    header.append(self.analysisDescription[i] + s + " 关键帧快退回放" + "\n")
                elif s=="03":
                    header.append(self.analysisDescription[i] + s + " 关键帧播放" + "\n")
                elif s=="04":
                    header.append(self.analysisDescription[i] + s + " 单帧上传" + "\n")
                count = count + self.analysis9201Length[i] * 2
            #快进或快退
            elif i==9:
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                if s=="00":
                    header.append(self.analysisDescription[i] + s + " 无效" + "\n")
                elif s=="01":
                    header.append(self.analysisDescription[i] + s + " 1倍" + "\n")
                elif s=="02":
                    header.append(self.analysisDescription[i] + s + " 2倍" + "\n")
                elif s=="03":
                    header.append(self.analysisDescription[i] + s + " 4倍" + "\n")
                elif s=="04":
                    header.append(self.analysisDescription[i] + s + " 8倍" + "\n")
                elif s=="05":
                    header.append(self.analysisDescription[i] + s + " 16倍"+ "\n")
                count = count + self.analysis9201Length[i] * 2
            elif i==10 or i==11:
                c_time = 0
                times = []
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                for j in range(0, 6):
                    time = s[c_time:c_time + 2]
                    times.append(time)
                    c_time = c_time + 2
                header.append(
                    self.analysisDescription[i] + times[0] + "-" + times[1] + "-" + times[2] + "-" + times[3] + "-" +
                    times[4] + "-" + times[5] + "\n")
                count = count + self.analysis9201Length[i] * 2
            else :
                s = protocolStr[count:count + (self.analysis9201Length[i] * 2)]
                header.append(self.analysisDescription[i] + s + "\n")
                count = count + self.analysis9201Length[i] * 2


        self.result=header

